layui.define([ 'element','form','scene','admin','storage'], function (exports) {
    let $ = layui.$,scene = layui.scene , element = layui.element;

    function main() {
        let api = '/develop/wiki';
        let query = {"sid":scene.gamesdk.game.sid}
        return scene.gamesdk.gzone.request(api,query).then(ret=>{
            main.apis = ret['ret'];
            let data = {'apis':showapi(ret['ret'])};
            let role = scene.gamesdk.game.role.get();
            data['sid'] = scene.gamesdk.game.sid;
            data['uid'] = role["_id"];
            data['role'] = role;
            data['server'] = scene.gamesdk.gzone.server.get();

            let _curUser=layui.storage('curUser');
            _curUser.set('role',{name:role.name,lv:role.lv});
            return data;
        })
    }

    main.finish = function () {
        layui['admin']();
        let article =  scene.gamesdk.game.config(1);


        let _curUser=layui.storage('curUser');
        let _role_list=layui.storage('userList');
        let _cuid=_curUser.get('uid');
        let _role_data=null;
        if(_role_list.get(_cuid)){
            _role_data=_role_list.get(_cuid);
        } else
        {
            _role_data=[];
        }
  
        let _user={zoneLoginData:_curUser.get('zoneLoginData'),zoneSelectData:_curUser.get('zoneSelectData'),
        gameSelectData:_curUser.get('gameSelectData'),opts:_curUser.get('opts'),role:_curUser.get('role')};

        let _has=false;
        for(let i in _role_data)
        {
            if(_role_data[i].zoneLoginData.code==_user.zoneLoginData.code)
            {
                _role_data[i]=_user;
                _has=true;
                break;
            }
        }
        if(!_has)
        {
             _role_data.push(_user);
        }
        _role_list.set(_cuid,_role_data);


        $('#GameApi').show();
        $('#DebugApi').hide();

        $('#GameApiLabel').click(function(event){
           $('#GameApi').toggle();
        }); 

        $('#DebugApiLabel').click(function(event){
           $('#DebugApi').toggle();
        }); 

 

       // _role_list[_curUser.]
        //_curUser.set('gameSelectData',uid);
    }

    function showapi(rows){
        let apis = {"/":{"name":"root","rows":[]},"debug":{"name":"debug","rows":[]} };
        for(let key in rows){
            let api = rows[key];
            let k,v,arr = key.split('/');
            if(!arr[2]){
                k = '/',v='root';
            }
            else{
                k = arr[1],v=k;
            }
            if(!apis[k]){
                apis[k] = {"name":v,"rows":[]}
            }
            apis[k]['rows'].push({"id":key,"name":api['desc'][0]||key  })
        }
        return apis;
    }


    exports('game/main', main);
})